/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Ubigeo;
import com.sgsmcan.Hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class UbigeoDaoImpl extends HibernateDao<Ubigeo> implements UbigeoDao<Ubigeo>{

    @Override
    public List<Ubigeo> getUbigeos() {
        List<Ubigeo> auxiliar = null;
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            auxiliar = session.createCriteria(Ubigeo.class).list();
        }catch(HibernateException e){
            e.printStackTrace();
        }finally{
            if(session != null){
                session.close();
            }
        }
        return auxiliar;
    }

    @Override
    public Ubigeo getUbigeo(String codigoDep, String codigoProv, String codigoDist) {
        Ubigeo ubigeo = null;
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria  criteria = session.createCriteria(Ubigeo.class,"ubi");
            criteria.add(Restrictions.eq("ubi.codigoDepartamento", codigoDep));
            criteria.add(Restrictions.eq("ubi.codigoProvincia", codigoProv));
            criteria.add(Restrictions.eq("ubi.codigoDistrito", codigoDist));
            criteria.addOrder(Order.asc("ubi.codigoDepartamento"));
            ubigeo = (Ubigeo) criteria.uniqueResult();
        }catch(HibernateException e){
            e.printStackTrace();
        }finally{
            if(session != null){
                session.close();
            }
        }
        return ubigeo;
    }

    @Override
    public Ubigeo getUbigeo(int id) {
        Ubigeo ubigeo = null;
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            ubigeo = (Ubigeo) session.get(Ubigeo.class, id);
        }catch(HibernateException e){
            e.printStackTrace();
        }finally{
            if(session != null){
                session.close();
            }
        }
        return ubigeo;
    }

    
    
}
